/// 用户认证模型 - User authentication model
class AuthUser {
  /// 用户ID - User ID
  final String id;
  
  /// 手机号 - Phone number
  final String? phone;
  
  /// 邮箱 - Email
  final String? email;
  
  /// 用户名 - Username
  final String? username;
  
  /// 头像URL - Avatar URL
  final String? avatar;
  
  /// 认证令牌 - Authentication token
  final String token;
  
  /// 刷新令牌 - Refresh token
  final String? refreshToken;
  
  /// 令牌过期时间 - Token expiration time
  final DateTime? tokenExpiry;

  const AuthUser({
    required this.id,
    this.phone,
    this.email,
    this.username,
    this.avatar,
    required this.token,
    this.refreshToken,
    this.tokenExpiry,
  });

  /// 从JSON创建AuthUser实例 - Create AuthUser instance from JSON
  factory AuthUser.fromJson(Map<String, dynamic> json) {
    return AuthUser(
      id: json['id'] ?? '',
      phone: json['phone'],
      email: json['email'],
      username: json['username'],
      avatar: json['avatar'],
      token: json['token'] ?? '',
      refreshToken: json['refresh_token'],
      tokenExpiry: json['token_expiry'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(json['token_expiry'])
          : null,
    );
  }

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'phone': phone,
      'email': email,
      'username': username,
      'avatar': avatar,
      'token': token,
      'refresh_token': refreshToken,
      'token_expiry': tokenExpiry?.millisecondsSinceEpoch,
    };
  }

  /// 检查令牌是否过期 - Check if token is expired
  bool get isTokenExpired {
    if (tokenExpiry == null) return false;
    return DateTime.now().isAfter(tokenExpiry!);
  }
}

/// 登录请求模型 - Login request model
class LoginRequest {
  /// 手机号 - Phone number
  final String? phone;
  
  /// 邮箱 - Email
  final String? email;
  
  /// 密码 - Password
  final String? password;
  
  /// 短信验证码 - SMS code
  final String? smsCode;

  const LoginRequest({
    this.phone,
    this.email,
    this.password,
    this.smsCode,
  });

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      if (phone != null) 'phone': phone,
      if (email != null) 'email': email,
      if (password != null) 'password': password,
      if (smsCode != null) 'smsCode': smsCode,
    };
  }

  /// 验证登录请求是否有效 - Validate login request
  bool get isValid {
    // 必须有手机号或邮箱 - Must have phone or email
    if (phone == null && email == null) return false;
    
    // 必须有密码或验证码 - Must have password or SMS code
    if (password == null && smsCode == null) return false;
    
    return true;
  }
}

/// 注册请求模型 - Register request model
class RegisterRequest {
  /// 手机号 - Phone number
  final String? phone;
  
  /// 邮箱 - Email
  final String? email;
  
  /// 密码 - Password
  final String password;
  
  /// 短信验证码 - SMS code
  final String? smsCode;
  
  /// 邮箱验证码 - Email code
  final String? emailCode;

  const RegisterRequest({
    this.phone,
    this.email,
    required this.password,
    this.smsCode,
    this.emailCode,
  });

  /// 转换为JSON - Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      if (phone != null) 'phone': phone,
      if (email != null) 'email': email,
      'password': password,
      if (smsCode != null) 'smsCode': smsCode,
      if (emailCode != null) 'emailCode': emailCode,
    };
  }

  /// 验证注册请求是否有效 - Validate register request
  bool get isValid {
    // 必须有手机号或邮箱 - Must have phone or email
    if (phone == null && email == null) return false;
    
    // 密码不能为空 - Password cannot be empty
    if (password.isEmpty) return false;
    
    // 如果有手机号，必须有短信验证码 - If has phone, must have SMS code
    if (phone != null && smsCode == null) return false;
    
    // 如果有邮箱，必须有邮箱验证码 - If has email, must have email code
    if (email != null && emailCode == null) return false;
    
    return true;
  }
}